<p class="entity-table">
    <?= $this->Html->link("&laquo; nazad", "/invoices", array( 'escape' => false ))?>
    
    <span class="actions visible form button-set">
    <? if (!empty($data['Invoice']['id'])): ?>
    <? $entityId = $data['Invoice']['id'] ?>
    
        <?= $this->Form->button('Štampaj', array(
            'type'    => 'button',
            'onclick' => "pdfFaktura({$entityId})"
        )) ?><? 
        
            if ($data['Invoice']['storno']): 
        
        ?><?= $this->Form->button('Od-Storniraj', array(
            'class'   => 'green',
            'onclick' => "if (confirm('Da li želite da od-stornirate fakturu?')){location.href = '/invoices/storno/{$entityId}/0'}"
        ))?><? 
            
            else:
             
        ?><?= $this->Form->button('Storniraj', array(
            'class'   => 'delete',
            'onclick' => "if (confirm('Da li želite da stornirate fakturu?')){location.href = '/invoices/storno/{$entityId}'}"
        ))?><?
             
            endif
             
        ?><?= $this->Form->button('Obriši', array(
            'class'   => 'delete',
            'onclick' => "if (confirm('Da li želite da stornirate kampanju?')){location.href = '/invoices/remove/{$entityId}'}"
        ))?>
    <? endif ?>
    </span>
</p>
<?= $this->Form->create('Invoice', array('url' => '/invoices/save')) ?>

    <?= $this->Form->hidden('id', array( 'value' => (int)@$data['Invoice']['id'])) ?>
    
    <p><?= $this->Form->label('number', 'Broj:') ?>
    <?= $this->Form->text('number', array(
        'value' => @$data['Invoice']['number']
    )) ?><?= $this->App->formError('number') ?></p>
    
    <p><?= $this->Form->label('title', 'Naslov:') ?>
    <?= $this->Form->text('title', array(
        'value' => @$data['Invoice']['title']
    )) ?></p>
    
    <p><?= $this->Form->label('date', 'Datum:') ?>
    <?= $this->Form->text('date', array(
        'value' => @$data['Invoice']['date'],
        'class' => 'datepicker',
        'required' => false
    ))?><?= $this->App->formError('date') ?>
    <?= $this->Form->label('date_valuta', 'Datum valute:', array('class'=>'auto')) ?>
    <?= $this->Form->text('date_valuta', array(
        'value' => @$data['Invoice']['date_valuta'],
        'class' => 'datepicker',
        'required' => false
    ))?><?= $this->App->formError('date_valuta') ?></p>
    
    <p><?= $this->Form->label('campaign_id', 'Kampanja:') ?>
    <?= $this->App->autocomplete('campaign_id', array(
        'label'  => @$data['Campaign']['label'],
        'value'  => @$data['Campaign']['id'],
        'url' => '/campaigns/search',
        'onSelect' => 'CampaignSelect',
        'onSearch' => 'CampaignSearch'
    )) ?><?= $this->App->formError('campaign_id') ?></p>
    
    <p><?= $this->Form->label('services', 'Usluge:', array('class'=>'left')) ?>
    <span class="left" id="invoice-services" style="margin-left:5px">
        <? if (! empty($data['InvoiceService'])): ?>
            <? foreach ($data['InvoiceService'] as $k=>$service): ?>
            <span style="display:block"><?= $this->Form->text("InvoiceService.{$k}.service", array(
                'value' => $service['service'], 
                'class'=>'large'
            )) ?></span>
            <? endforeach ?>
        <? endif ?>
    </span></p>
    
    <br clear="all">
    <p><?= $this->Form->label('comment', 'Napomena:', array('class'=>'left')) ?>
    <?= $this->Form->textarea('comment', array(
        'value' => @$data['Invoice']['comment']
    ))?></p>

    <?= $this->Form->button('Sačuvaj') ?>
<?= $this->Form->end() ?>

<script type="text/javascript">
function CampaignSelect(el) {
    var CampaignId = $(el).siblings('input[type=hidden]').eq(0).val();
    var container  = $('#invoice-services');
    
    $.ajax({
        url: '/campaigns/data/' + CampaignId,
        beforeSend: function() {
            container.html('učitavam usluge...');
        },
        complete: function(req) {
            container.empty();
            if (req.status == 200) {
                var data = eval('(' + req.responseText + ')');
                if (data.Location && data.Location.length>0) {
                    var location, item, input;
                    
                    for (i=0; i<data.Location.length; i++) {
                        location = data.Location[i];
                        
                        var adTypes = []
                        for (x in location.AdType) {
                            if (adTypes.indexOf() < 0) {
                                adTypes.push(location.AdType[x].name)
                            }
                        }
                        var ad = adTypes.length>1 ? 'reklame' : 'reklama';
                        var value = adTypes.join(", ") + ' ' + ad + ' na lokaciji ' + location.name;
                        
                        item  = $('<span style="display:block">').appendTo(container);
                        input = $('<input type="text" class="large">')
                                    .appendTo(item)
                                    .attr('name', 'data[InvoiceService]['+i+'][service]')
                                    .val(value)
                    }
                }
            }
        }
    })
}
function CampaignSearch(el) {
    $('invoice-services').empty();
}
</script>
